#ifndef __TOOLKIT_H__

#define         __TOOLKIT_H__
extern "C"
{
	#include <stdio.h>
	#include <string.h>
	#include <malloc.h>
	#include <stdlib.h>
	#include <stdarg.h>
}

//#define			_DEBUG
#ifdef WIN32
#define	strcasecmp	stricmp
#define	strncasecmp     strnicmp
#endif
#if 0
#ifndef	MIN
#define	MIN(x,y)	(((x) < (y))?(x) : (y))
#endif	/* MIN */
#ifndef	MAX

#define	MAX(x, y)	(((x) > (y))?(x) : (y))
#endif	/* MAX */

#endif
#ifndef BCD
#define		BCD(c1,c2)	((c1<<4) | c2)
#endif

typedef		void *			LPVOID;
typedef		unsigned long	DWORD;
typedef		unsigned short	SHORT;
typedef		unsigned char	uchar;

typedef struct _ListNode {
	void * m_pItem;
	struct _ListNode * m_pNext;
	struct _ListNode * m_pPrev;
} NODE, *LPNODE;

class CPtrList
{
protected:
	int m_nCount;
	LPNODE m_pHead, m_pTail;
	LPNODE m_pIterator;
	
public:
	CPtrList();
	virtual ~CPtrList();
	
	LPVOID DeleteNode(LPNODE &pNode);

	virtual int Add(LPVOID pItem);
	virtual LPVOID RemoveHead() { return Delete(0); }
	virtual int Delete(LPVOID pItem);
	virtual LPVOID Delete(int nIndex);
	virtual LPVOID Item(int nIndex);
	
	int Count() const;
	
	virtual LPVOID getHead();
	virtual LPVOID getTail();
	virtual LPVOID getNext();
	virtual	LPVOID getPrev();

	virtual LPVOID GetNext(LPNODE &pNode);
	virtual LPVOID GetPrev(LPNODE &pNode);
	virtual LPNODE GetHead();
	virtual LPNODE GetTail();

	virtual int IndexOf(LPVOID pItem);

protected:

};

typedef		char	*LPSTR, *LPCSTR;
#define		between(a,x,b)		(((x)>=(a))&&((x)<(b)))

class CString
{
public:
	CString();
	CString(char *str);
	CString(CString &s);
	virtual ~CString();

	CString operator+ (char *str);
	CString operator+ (CString &s);

	void operator+= (char *str) { __Plus( str ); }
	void operator+= (CString & s) { __Plus( s.m_ptr ); }

	void operator=  (char *str) { __Copy( str ); }
	void operator= (CString & s) { __Copy( s.m_ptr ); }
	
	int operator== (char *str) { return __Compare(str); }
	int operator== (CString s) { return __Compare(s.m_ptr); }
	char & operator[](int nIndex);

	operator LPSTR() const { return m_ptr; }

	char	*GetBuffer(int nMinBufLength);
	void	MakeLower();
	void	MakeUpper();
	void	MakeReverse();
	int		IgnoreCaseCompare(char *str);
	void	Format(char *format, ...);
	void	Append(char *format, ...);
	int		GetLength() const { return m_actsize; }

	CString	Left( int l);
	CString Right( int l);
	CString Mid( int s,  int l);


	// trimming whitespace (either side)
	void	AllTrim();
	void	TrimRight();
	void	TrimLeft();

	// searching (return starting index, or -1 if not found)
	// look for a single character match
	int		Find(char ch) const;               // like "C" strchr
	int		ReverseFind(char ch) const;

	// look for a specific sub-string
	int		Find(char* lpszSub) const;        // like "C" strstr
protected:
	void	Release();
	void	__Format(char *format, va_list va);
	void	__Plus(char *str);
	void	__Copy(char *str);
	int	__Compare(char *str);
private:
	char	*m_ptr, m_refch;
	int		m_actsize;
	int		m_sparesize;
	int		m_maxsize;
};

class CStringList : public CPtrList
{
public:
	CStringList();
	virtual ~CStringList();
	
	virtual int Add(LPVOID pItem);
	virtual int IndexOf(LPVOID pItem);

	char * Strings( int wIndex );
	void	Clear();
	void	Display();
};

extern	void	ReadFileStr(FILE *FS, char *buf);
extern	char	*alltrim(char *src);
extern  void	substr(char * src, char * dst, int b, int l);
extern	int	 IntToBCD(unsigned char * tempchar,int n , int ctrl);
extern	int	 BCDToInt(uchar *KeyBuf, int ctrl);
extern	char	ParseStr(const char *Buf, int &st, int dstlen, char *Dest);
extern	int	myatoi(char *val);
extern	int	MyStrToInt(const char *s);
extern	double	MyStrToFloat(const char *s);
extern	int	getHexValue(char ch);
extern	void	decodeHttpCode(char * httpcode);
extern	void	ParseToLine(CStringList *res, char *buf, int bytes);
extern	void	ParseToItem(CStringList *res, char *line, int bytes, char ch);
extern	char *	gettoken(char **src, char *dst, char token);
extern	int	parseAddress(char *src, char *addr, int &port);

#endif //       end of __TOOLKIT_H__

/*
 * $Log: comutil.h,v $
 * Revision 1.1.1.1  2003/12/08 02:53:10  administrator
 * no message
 *
 * Revision 1.2  2003/11/26 02:33:27  administrator
 * backup 2003-11-26
 * In this version:
 * 1) NETAT200 Driver was modified by Fish, But It's Wrong!
 * 2) Philips Driver added!
 * 3) panasonis sx560 untested!
 *
 * Revision 1.5  2001/05/20 20:25:24  cvsroot
 * *** empty log message ***
 *
 * Revision 1.4  2001/05/07 09:24:42  cvsroot
 * before go to shenzhen
 *
 * Revision 1.3  2001/04/25 17:35:25  cvsroot
 * add control code for stopserver
 *
 * Revision 1.2  2001/01/23 22:00:46  cvsroot
 * Add Log Message in CVS
 *
 *
*/
